package com.xh.bussiness.system.controller;

import com.xh.bussiness.system.model.Dictionary;
import com.xh.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xh.bussiness.system.bean.dto.DictionaryBean;
import com.xh.bussiness.system.bean.request.DictionaryRequest;
import com.xh.bussiness.system.service.IDictionaryService;
import com.xh.bussiness.system.service.IDictionaryTypeService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.controller.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 数据字典表 前端控制器
 * </p>
 *
 * @author daiwei
 * @since 2018-10-10
 */
@Api(tags="数据字典")
@RestController
@RequestMapping("/system/dictionary")
public class DictionaryController extends BaseController {

    @Autowired
    private IDictionaryService dictionaryService;
    @Autowired
    private IDictionaryTypeService dictionaryTypeService;
    

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @ApiOperation(value="数据字典数据分页查询", notes = "数据字典数据分页查询")
    @RequestMapping("/queryPage")
    public XhPage queryPage(XhPage page, DictionaryRequest bean){
        page=dictionaryService.queryDictionaryPage(page,bean);
        return page;
    }

    /**
     * 数据查询
     * @param typeCode 数据字典编码
     * @return
     */
    @ApiOperation(value="数据字典数据查询", notes = "数据字典数据查询-只返回根节点数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="typeCode", value="数据字典编码", required = true, dataType = "String"),
    })
    @RequestMapping("/queryRoot")
    public AjaxResult query(String typeCode){
        DictionaryRequest bean=new DictionaryRequest();
        bean.setParentId("root");
        bean.setTypeCode(typeCode);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",dictionaryService.queryDictionary(bean));
    }

    /**
     * 数据查询
     * @param typeCode 数据字典编码
     * @return
     */
    @ApiOperation(value="数据字典数据查询", notes = "数据字典数据查询-返回所有匹配数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="typeCode", value="数据字典编码", required = true, dataType = "String"),
    })
    @RequestMapping("/queryAll")
    public AjaxResult queryAll(String typeCode){
        DictionaryRequest bean=new DictionaryRequest();
        bean.setTypeCode(typeCode);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",dictionaryService.queryDictionary(bean));
    }

    /**
     * 新增和保存
     * @param dictionaryBean 实体对象
     * @return
     */
    @ApiOperation(value="数据字典数据保存", notes = "数据字典数据保存")
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated DictionaryBean dictionaryBean){
        //判重
        List<Dictionary> dictionaryList = dictionaryService.queryByName(dictionaryBean.getTypeCode(), dictionaryBean.getName());
        if(dictionaryList.size()>0){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"名称重复");
        }
        if(StringUtils.isEmpty(dictionaryBean.getId())){
            dictionaryService.saveObj(dictionaryBean);
        }else{
            dictionaryService.modifyObj(dictionaryBean);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",dictionaryBean);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @ApiOperation(value="数据字典数据删除", notes = "数据字典数据删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id", value="数据字典id", required = true, dataType = "String"),
    })
    @PostMapping("/deleteById")
    @Transactional
    public AjaxResult deleteById(String id){
        dictionaryService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @ApiOperation(value="数据字典数据批量删除", notes = "数据字典数据批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name="ids", value="数据字典id集合", required = true, dataType = "String[]"),
    })
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        dictionaryService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    @ApiOperation(value = "数据字典数据批量启用停用", notes = "数据字典数据批量启用停用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "数据字典id集合", required = true, dataType = "String[]"),
            @ApiImplicitParam(name = "status", value = "状态(0启用，1停用)", required = true, dataType = "Integer")
    })
    @PostMapping("/batchChangeStatus")
    @Transactional
    public AjaxResult batchChangeStatus(String[] ids, Integer status) {
        dictionaryService.batchChangeStatus(ids, status);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 查询子节点
     * @param typeCode 数据字典编码
     * @return
     */
    @ApiOperation(value="查询子节点", notes = "数据字典数据查询-返回所有匹配数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="parentType", value="父级节点类型", required = true, dataType = "String"),
            @ApiImplicitParam(name="parentCode", value="父级节点编码", required = true, dataType = "String"),
            @ApiImplicitParam(name="childType", value="子节点类型", required = true, dataType = "String"),
    })
    @RequestMapping("/queryChilds")
    public AjaxResult queryChilds(String parentType, String parentCode, String childType){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",dictionaryService.queryChilds(parentType, parentCode, childType));
    }
    
    /**
     * 查询子节点
     * @param typeCode 数据字典编码
     * @return
     * @throws Exception 
     */
    @ApiOperation(value="查询当前用户有权限的学科", notes = "查询当前用户有权限的学科")
    @RequestMapping("/querySubject")
    public AjaxResult querySubject() throws Exception{
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,dictionaryService.querySubject());
    }
    
    /**
     * 查询子节点
     * @param typeCode 数据字典编码
     * @return
     * @throws Exception 
     */
    @ApiOperation(value="查询所有年级", notes = "查询所有年级")
    @RequestMapping("/queryGrade")
    public AjaxResult queryGrade() throws Exception{
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,dictionaryService.queryGrade());
    }
    
    /**
     * 查询子节点
     * @param typeCode 数据字典编码
     * @return
     * @throws Exception 
     */
    @ApiOperation(value="查询所有年段", notes = "查询所有年段")
    @RequestMapping("/queryGradeRange")
    public AjaxResult queryGradeRange() throws Exception{
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,dictionaryService.queryGradeRange());
    }
    
    /**
     * 
     * @return
     * @throws Exception
     */
    @ApiOperation(value="从菁优网拉取最新学科数据，更新本地数据")
    @RequestMapping("/initSubject")
    public AjaxResult initSubject() throws Exception{
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,dictionaryService.initSubject());
    }
    /**
     * 
     * @return
     * @throws Exception
     */
    @ApiOperation(value="从菁优网拉取最新年级数据，更新本地数据")
    @RequestMapping("/initGrade")
    public AjaxResult initGrade() throws Exception{
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,dictionaryService.initGrade());
    }

    @ApiOperation(value="查询学段下面的科目")
    @RequestMapping("/queryGradeAndSubject")
    public AjaxResult queryGradeAndSubject(){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,dictionaryService.queryGradeAndSubject());
    }

    @ApiOperation(value="查询学段下面的数据")
    @RequestMapping("/queryDataByType")
    public AjaxResult queryDataByType(String code,String typeCode){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,dictionaryService.queryDataByType(code,typeCode));
    }


    @ApiOperation("分页查询学科")
    @RequestMapping("/querySubjectPage")
    public XhPage querySubjectPage(XhPage page, String subjectName, String gradeRangeCode) {
        return dictionaryService.querySubjectPage(page, subjectName, gradeRangeCode);
    }

    @ApiOperation("通过类型集合查询数据")
    @RequestMapping("/queryDictByCodes")
    public AjaxResult queryDictByCodes(String typeCodesStr) {
        if (StringUtils.isEmpty(typeCodesStr)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, dictionaryService.queryDictByTypeCodesToMap(Arrays.asList(typeCodesStr.split(","))));
    }
}

